package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.pojo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
@Service
@Slf4j
public class PermissionServiceImpl implements  PermissionService {
   @Autowired
    PermissionDao permissionDao;
    @Override
    public Set<Permission> findAll() {
        log.debug("permissionServiceImpl的findAll方法被访问了");
        Set<Permission> permissions = permissionDao.findAll();
        log.debug("permissionServiceImpl的findAll方法查询成功:"+permissions);
        return permissions;
    }

    @Override
    public Set<Integer> findPermissionByRoleId(int rid) {
        log.debug("permissionServiceImpl的findPermissionByRoleId被访问了");
        Set<Integer> permissionsByRoleId = permissionDao.findPermissionsByRoleId2(rid);
        log.debug("permissionServiceImpl的findPermissionByRoleId被访问了成功了:"+permissionsByRoleId);

        return  permissionsByRoleId;
    }








    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void delById(Integer id) {
        long count = permissionDao.findCountByRoleId(id);
        if(count > 0){
            log.debug("当前权限被使用，不能删除!!!");
            //当前检查项被引用，不能删除
            throw new BusinessRuntimeException("该权限已被赋予，不能删除!!!");
        }
           permissionDao.delById(id);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        //1. 开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2. 条件查询
        Page<Permission> page = permissionDao.findByCondition(queryPageBean.getQueryString());

        //封装PageResult
        return new PageResult(page.getTotal(), page);

    }

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public void edit(Permission permission) {

        permissionDao.edit(permission);
    }


}
